#include "./all.h"
#define __ZHUCEHANSHU__
#define __DENGRUHANSHU__
//线程处理函数
void * do_clint(void *argv){
	/* int connfd = *(int *(argv)); //接受参数 */
	int connfd  = *((int *)argv);
	int ret; //返回值判断
	pthread_t pid = pthread_self(); //获取线程ID
	pthread_detach(pid);
	messageText *message = (messageText *)malloc(sizeof(messageText));//创建抽象对象
	while(1){
		ret = recv(connfd,message,sizeof(messageText),0);
		if(ret <= 0){
			printf("客户端退出\n");
			printf("___%d___,__%s__",__LINE__,__FILE__);
			free(message);
			close(connfd); //回收通信流资源
			pthread_cancel(pid); //退出线程
		}
		switch (message->flags){
		case DENGRU:
#ifdef __DENGRUHANSHU__
			enterFunction(connfd,message);
			break;
#endif
		case ZHUCE:
#ifdef __ZHUCEHANSHU__
			registerUser(connfd,message);
			break;
#endif
		case SHANCHU:
			break;
		case XIUGAI:
			break;
		case PUTONGYONGHUXIUGAIXINXI:
#ifdef __PUTHONGCHANGE__
			putongChange(connfd,message);
			break;
#endif
		default:
			break;
		}
	}
	free(message);
	pthread_cancel(pid);
}
//操作数据库函数
/*
 *注册函数
 * */
#ifdef  __ZHUCEHANSHU__
void registerUser(int connfd,messageText *message){
	//获取当前时间戳
	struct timeval stamp;
	gettimeofday(&stamp,NULL);
	sqlite3 *db;	 //操作数据库流
	char *errmsg;
	int ret;
	char buf[256] = {0};
	message->mod = ORDINARY; //默认注册为普通用户
	message->solary = 0; //入职基础工资
	message->id = stamp.tv_usec; //初始化工号
	//打开数据库
	ret = sqlite3_open("./Message.db",&db);
	if(ret != SQLITE_OK){
		message->ret = NO;
		send(connfd,message,sizeof(messageText),0);
		sqlite3_close(db);
	}else{

		//插入数据
		sprintf(buf,"insert into manyMessage values ('%d','%d','%d','%d','%s','%d');",
				message->id,message->passwd,message->age,message->solary,
				message->name,message->mod);		
		//插入成功,返回userMessage的返回值接受标志位
		ret = sqlite3_exec(db,buf,NULL,NULL,&errmsg);	
		if(ret != SQLITE_OK){
			printf("Insert error...\n");
			message->ret = NO;
			send(connfd,message,sizeof(messageText),0);
			sqlite3_free(errmsg);
			sqlite3_close(db);
			db = NULL;
			errmsg = NULL;
			pthread_cancel(pthread_self());
		}else{
			message->ret = YES;	
			send(connfd,message,sizeof(messageText),0);
			sqlite3_free(errmsg);
			sqlite3_close(db);
			db = NULL;
			errmsg = NULL;
			//关闭数据库
		}
	}
}
#endif
#ifdef __DENGRUHANSHU__
int enterFunction(int connfd,messageText*message){
	sqlite3 *db = NULL;	
	char *errmsg;
	char **resultp;
	int nrow;
	int ncolumn;
	int ret;
	//打开数据库
	ret = sqlite3_open("./Message.db",&db);		
	if(ret != SQLITE_OK){
		message->ret = NO;
		send(connfd,message,sizeof(message),0);
	}
	char buf[256] = {0};
	sprintf(buf,"select * from manyMessage where id = '%d' and passwd = '%d';",message->id,message->passwd);
	ret = sqlite3_get_table(db,buf,&resultp,&nrow,&ncolumn,&errmsg);	
	if(ret != SQLITE_OK){
		message->ret = NO;
		send(connfd,message,sizeof(messageText),0);
		sqlite3_free(errmsg);
		sqlite3_close(db);
		return 0;
	}
	message->id = atoi(resultp[6]);
	message->passwd = atoi(resultp[7]);
	message->age  = atoi(resultp[8]);
	message->solary = atoi(resultp[9]);
	strcpy(message->name,resultp[10]);
	message->mod = atoi(resultp[11]);
	printf("mod = %d...\n",message->mod);
	message->ret = YES;
	send(connfd,message,sizeof(messageText),0);
	sqlite3_free_table(resultp);
	sqlite3_free(errmsg);
	sqlite3_close(db);
	//讲输入的账号密码与数据库中的进行配对
	//配对失败，返回ret的值
	//配对成功返回相应的模式为，让客户端进行页面选择
}
#endif
#ifdef __PUTHONGCHANGE__
//普通用户修改信息函数
void putongChange(int connfd,messageText*message){
	//变量
	sqlite3 *db = NULL;	//数据库操作流
	char *errmsg; //出错信息
	char **resultp;//存放搜寻到的信息
	int nrow;     //行数
	int ncolumn;  //列数
	int ret;     //返回值判断
	//打开数据库
	ret = sqlite3_open("./Message.db",&db);
	if(ret != SQLITE_OK){
		message->ret = NO;
		send(connfd,message,sizeof(message),0);
	}else{
		char buf[256];
		sprintf(buf,"update manyMessage set name = '%s',age = '%d',passwd = '%d' where id = '%d' ",message->name,message->age,message->passwd, message->id);
		ret = sqlite3_get_table(db,buf,&resultp,&nrow,&ncolumn,&errmsg);
		if(ret != SQLITE_OK){
			message->ret = NO;
			send(connfd,message,sizeof(messageText),0);
			sqlite3_free(errmsg);
			sqlite3_close(db);

		}else{
			message->ret = YES;
			send(connfd,message,sizeof(messageText),0);
			sqlite3_free_table(resultp);
			sqlite3_free(errmsg);
			sqlite3_close(db);
		}
	}
}
#endif
